package com.example.devsa_platform.pojo.doc;

import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.apache.commons.csv.CSVRecord;
import org.springframework.data.elasticsearch.annotations.Document;
import org.springframework.data.elasticsearch.annotations.Field;

import java.io.Serializable;
import java.util.Objects;

@Data
@AllArgsConstructor
@NoArgsConstructor
@JsonIgnoreProperties(ignoreUnknown = true) //解决索引中字段和实体类非一一对应的而引起的JSON解析问题
@Document(indexName = "dev_base_info",createIndex = false)
public class BaseInfoDoc implements Serializable {
    @Field(name = "main_branch")
    private String mainBranch;
    @Field(name = "age")
    private String age;
    @Field(name = "employment")
    private String employment;
    @Field(name = "remote_work")
    private String remoteWork;
    @Field(name = "coding_activities")
    private String codingActivities;

    public BaseInfoDoc(CSVRecord record) {
        this.mainBranch = getValueOrDefault(record.get("MainBranch"));
        this.age = getValueOrDefault(record.get("Age"));
        this.employment = getValueOrDefault(record.get("Employment"));
        this.remoteWork = getValueOrDefault(record.get("RemoteWork"));
        this.codingActivities = getValueOrDefault(record.get("CodingActivities"));
    }

    private String getValueOrDefault(String value) {
        return Objects.equals(value, "NA") ? "No Answer" : value;
    }
}
